package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.PageResult;
import com.itheima.domain.pojo.Students;
import com.itheima.domain.pojo.StudentsQueryParam;
import com.itheima.mapper.StudentsMapper;
import com.itheima.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
public class StudentsServiceImpl  implements StudentsService {
    @Autowired
    private StudentsMapper studentsMapper;
    @Override
    public PageResult<Students> page(StudentsQueryParam studentsQueryParam) {
        PageHelper.startPage(studentsQueryParam.getPage(), studentsQueryParam.getPageSize());
        List<Students> studentsList = studentsMapper.list(studentsQueryParam);
        Page<Students> p =(Page<Students>)studentsList;
        return new PageResult<>(p.getTotal(), studentsList);
    }

    //新增学员
    @Override
    public void add(Students students) {
        students.setCreateTime(LocalDate.now());
        students.setUpdateTime(LocalDate.now());
        studentsMapper.add(students);
    }
    //批量删除员工
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> id) {
        //批量删除员工基本信息
        studentsMapper.delete(id);
    }

    @Override
    public Students getById(Integer id) {
        return studentsMapper.getById(id);
    }

    //根据id修改员工
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Students students) {
        students.setUpdateTime(LocalDate.now());
        studentsMapper.update(students);
    }

    //违纪处理
    @Override
    public void violation(Integer id, Integer score) {
        studentsMapper.violation(id, score);
    }
}
